
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<style>
			body {
				background: black;
				color: rgb(80, 80, 80);
			}
			body, pre, #legend span {
				font-family: Menlo, monospace;
				font-weight: bold;
			}
			#topbar {
				background: black;
				position: fixed;
				top: 0; left: 0; right: 0;
				height: 42px;
				border-bottom: 1px solid rgb(80, 80, 80);
			}
			#content {
				margin-top: 50px;
			}
			#nav, #legend {
				float: left;
				margin-left: 10px;
			}
			#legend {
				margin-top: 12px;
			}
			#nav {
				margin-top: 10px;
			}
			#legend span {
				margin: 0 5px;
			}
			.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }

		</style>
	</head>
	<body>
		<div id="topbar">
			<div id="nav">
				<select id="files">
				
				<option value="file0">basic\basic\basic.go (4.9%)</option>
				
				</select>
			</div>
			<div id="legend">
				<span>not tracked</span>
			
				<span class="cov0">not covered</span>
				<span class="cov8">covered</span>
			
			</div>
		</div>
		<div id="content">
		
		<pre class="file" id="file0" style="display: none">package basic

import (
        "fmt"
        "math"
        "math/cmplx"
)

// 函数外部必须使用var等关键字(包内部变量.没有全局变量)
var name = "aqie"

// 定义多个变量
var (
        aa = 1
        bb = 2
        cc = 3
)

func main() <span class="cov0" title="0">{
        fmt.Println("hello world!")
        variableZeroValue()
        variableInitialValue()
        variableTypeDeduction()
        variableShorter()

        fmt.Println(aa, bb, cc)
        euler()

        triangle()
        consts()
        enums()
}</span>

func variableZeroValue() <span class="cov0" title="0">{
        var a int
        var s string
        fmt.Printf("%d %q\n", a, s)
}</span>

func variableInitialValue() <span class="cov0" title="0">{
        var a, b int = 3, 4
        var s string = "aqie"
        fmt.Println(a, b, s)
}</span>

func variableTypeDeduction() <span class="cov0" title="0">{
        var a, b, c, s = 3, 4, true, "aqie"
        fmt.Println(a, b, c, s)
}</span>

func variableShorter() <span class="cov0" title="0">{
        a, b, c, s := 3, 4, true, "aqie" // 第一次定义使用 :=
        c = false
        fmt.Println(a, b, c, s)
}</span>

func euler() <span class="cov0" title="0">{
        c := 3 + 4i
        fmt.Println(cmplx.Abs(c)) // 取模

        fmt.Println(cmplx.Pow(math.E, 1i*math.Pi) + 1) // 欧拉公式 e 的i*π次方
        fmt.Println(cmplx.Exp(1i*math.Pi) + 1)
        fmt.Printf("%.3f\n", cmplx.Exp(1i*math.Pi)+1) // 只要小数点后三位
}</span>

// 类型转换
// c = int(math.Sqrt(float64(a*a + b*b))) // 求平方根(note: float 可能会出现4.9999)
func triangle() <span class="cov0" title="0">{
        var a, b int = 3, 4
        fmt.Println(calcTriangle(a,b))
}</span>

func calcTriangle(a,b int ) int<span class="cov8" title="1">{
        c := int(math.Sqrt(float64(a*a + b*b)))
        return c
}</span>

// 常量
func consts() <span class="cov0" title="0">{
        const filename = "abc.txt" // 也可以使用括号定义一组
        const a, b = 3, 4          // 常量不定义类型类似文本替换,既能做int也能做float
        var c int
        c = int(math.Sqrt(a*a + b*b))
        fmt.Println(c, filename)
}</span>

// 枚举, 直接定义为const
func enums() <span class="cov0" title="0">{
        const (
                cpp    = 0
                java   = 1
                python = 2
                golang = 3
        )
        fmt.Println(cpp, java, python, golang)

        const (
                spring = iota // 代表自增值枚举类型
                summer
        )
        fmt.Println(spring, summer)

        // 位运算
        const (
                b = 1 &lt;&lt; (10 * iota) // 左移 10 * iota, 2 的十次方
                kb
                mb
                gb
                tb
                pb
        )
        fmt.Println(b, kb, mb, gb, tb, pb)
}</span>
</pre>
		
		</div>
	</body>
	<script>
	(function() {
		var files = document.getElementById('files');
		var visible;
		files.addEventListener('change', onChange, false);
		function select(part) {
			if (visible)
				visible.style.display = 'none';
			visible = document.getElementById(part);
			if (!visible)
				return;
			files.value = part;
			visible.style.display = 'block';
			location.hash = part;
		}
		function onChange() {
			select(files.value);
			window.scrollTo(0, 0);
		}
		if (location.hash != "") {
			select(location.hash.substr(1));
		}
		if (!visible) {
			select("file0");
		}
	})();
	</script>
</html>
